/*
 * DbHelper.java
 *
 * Created on April 13, 2007, 1:41 PM
 */

package CSIS543TFinalProject.db;

import java.util.List;
import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.query.Predicate;
import CSIS543TFinalProject.*;
import com.db4o.ObjectSet;
import com.db4o.query.Query;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * this class just wraps some of the more common db operations and constants that
 * we need throughout the application.
 * @author dwight fellman
 */
public class DbHelper {
   ObjectContainer db;
   public DbHelper() {
      db = Db4o.openFile(DbHelper.getDegreePlannerDbName());
   }
   
   public static String getDegreePlannerDbName() {
      return "degreePlanner.db";
   }
   
   /**
    * return a Degree object with the given name and concentration.
    */
   public Degree GetDegree(final String name, final String concentration) {
      List <Degree> degrees = db.query(new Predicate<Degree>() {
         public boolean match(Degree degree) {
            boolean b = degree.getName().equals(name) && degree.getConcentration().equals(concentration);
            return b;
         }
      });
      return degrees.get(0);
   }
   
   /**
    * return a Course object with the given name
    */
   public Course GetCourse(final String name) {
      List <Course> courses = db.query(new Predicate <Course>() {
         public boolean match(Course c) {
            boolean b = c.getName().equals(name);
            return b;
         }
      });
      return courses.get(0);
   }
   
   /**
    * return all the Degree objects in the database
    */
   public List <Degree> GetDegrees() {
      List <Degree> degrees = db.query(new Predicate<Degree>() {
         public boolean match(Degree degree) {
            return true;
         }
      });
      return degrees;
   }
   
   /**
    * return all the Degree objects in the database, sorted by name and concentration
    */
   public List <Degree> GetDegreesSorted() {
      List <Degree> degrees = new ArrayList <Degree> ();
      Query query = db.query();
      query.constrain(Degree.class);
      query.descend("Concentration").orderAscending();
      ObjectSet result = query.execute();
      while(result.hasNext()) {
         degrees.add((Degree) result.next());
      }
      return degrees;
   }
   
   /**
    * return all the Course objects in the database
    */
   public List <Course> GetCourses() {
      List <Course> courses = db.query(new Predicate<Course>() {
         public boolean match(Course course) {
            return true;
         }
      });
      return courses;
   }
   
   /**
    * return all the Course objects in the database, sorted by course name
    */
   public List <Course> GetCoursesSorted() {
      List <Course> courses = new ArrayList <Course> ();
      Query query = db.query();
      query.constrain(Course.class);
      query.descend("Name").orderAscending();
      ObjectSet result = query.execute();
      while(result.hasNext()) {
         courses.add((Course) result.next());
      }
      return courses;
   }
   
   /**
    * for the given degree (name + concentration), return a list of required
    * course objects.
    */
   public List <Course> GetRequredCourses(String degreeName, String degreeConcentration) {
      Degree degree = GetDegree(degreeName, degreeConcentration);
      return degree.getRequiredCourses();
   }
   
   /**
    * close the underlying db4o database.
    * during development, was simply unable to tell with 100% certainty when
    * exactly this is required...
    */
   public void close() {
      db.close();
   }
}